package com.blueocean_health.care.management.service.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.OrderFlowDto;
import com.blueocean_health.care.management.em.FeeTypeEnum;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.em.OrderTimeTypeEnum;
import com.blueocean_health.care.management.em.TransTypeEnum;
import com.blueocean_health.care.management.mapper.OrderChargeInfoMapper;
import com.blueocean_health.care.management.service.OrderChargeInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 订单流水表业务实现类  2017年8月23日17:32:56
 * @author wuyanli
 *
 */
@Service
public class OrderChargeInfoServiceImpl implements OrderChargeInfoService{
	
	@Autowired
	private OrderChargeInfoMapper orderFlowDao;
	
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

	@Override
	public PageInfo<OrderFlowDto> getOrderFlowList(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
//		map.put("hospitalId", "4");
		String orderId = map.get("orderId");
		if(!StringUtils.isEmpty(orderId)){
			map.put("orderId", orderId.trim());
		}
		if(!StringUtils.isEmpty(map.get("workerName"))){
			map.put("workerName", map.get("workerName").trim());
		}
		int pageSize = Integer.parseInt(map.get("pageSize"));
		int page = Integer.parseInt(map.get("page"));
		PageHelper.startPage(page,pageSize,false);
		List<OrderFlowDto> orderFlowList = orderFlowDao.getOrderFlowList(map);
		long orderFlowTotal = orderFlowDao.getOrderFlowTotal(map);
		for (OrderFlowDto orderFlowDto : orderFlowList) {
			//通过Id获取收费方式名称
			String feeType = orderFlowDto.getFeeType();
			String val = StringUtils.isEmpty(feeType)?"": FeeTypeEnum.valueOf("_"+feeType).getVal();
			orderFlowDto.setFeeName(val);
			//通过Id 获取资金类型名称
			String transType = orderFlowDto.getTransType();
			String val2 = StringUtils.isEmpty(transType)?"":TransTypeEnum.valueOf("_"+transType).getVal();
			orderFlowDto.setTransName(val2);
			if("-0.00".equals(orderFlowDto.getTotalPay())){
				orderFlowDto.setTotalPay("0.00");
				
			}
			//时间类型  2018年6月7日15:18:49
			orderFlowDto.setTimeTypeName(OrderTimeTypeEnum.valueOf("_"+orderFlowDto.getTimeType()).getVal());
		}
		PageInfo<OrderFlowDto> pageInfo = new PageInfo<OrderFlowDto>(orderFlowList);
		pageInfo.setTotal(orderFlowTotal);
		return pageInfo;
	}

	/**
	 * 导出所有的订单流水列表 Excel 2017年8月25日15:30:11
	 */
	@Override
	public List<OrderFlowDto> getAllOrderFlowExcel(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
		List<OrderFlowDto> orderFlowList = orderFlowDao.getOrderFlowList(map);
		for (OrderFlowDto orderFlowDto : orderFlowList) {
			//通过Id获取对应收入类型名称
			String feeType = orderFlowDto.getFeeType();
			String val = StringUtils.isEmpty(feeType)?"": FeeTypeEnum.valueOf("_"+feeType).getVal();
			orderFlowDto.setFeeName(val);
			//通过Id获取对应资金类型名称
			String transType = orderFlowDto.getTransType();
			String val2 = StringUtils.isEmpty(transType)?"":TransTypeEnum.valueOf("_"+transType).getVal();
			orderFlowDto.setTransName(val2);
//			//时间类型  2018年6月7日15:18:49
			orderFlowDto.setTimeTypeName(OrderTimeTypeEnum.valueOf("_"+orderFlowDto.getTimeType()).getVal());
		}
		return orderFlowList;
	}

	@Override
	public Map<String,Object> getAmountCount(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
//		map.put("hospitalId", "4");
//		if(map.get("amountMin") == null || "".equals(map.get("amountMin"))){
//			map.put("amountMin", "-"+Double.MAX_VALUE);
//		}
//		if(map.get("amountMax") == null || "".equals(map.get("amountMax"))){
//			map.put("amountMax", Double.MAX_VALUE+"");
//		}
		Map<String,Object> map1 = orderFlowDao.getAmountCount(map);
		DecimalFormat df = new DecimalFormat("0.00");
		String format = "0.00";
		if(!StringUtils.isEmpty(map1)){
			if(!StringUtils.isEmpty(map1.get("amountCount"))){
				map1.put("amountCount", df.format(map1.get("amountCount")));
			}else{
				map1.put("amountCount", format);
			}
			if(!StringUtils.isEmpty(map1.get("totalPayCount"))){
				map1.put("totalPayCount", df.format(map1.get("totalPayCount")));
			}else{
				map1.put("totalPayCount", format);
			}
		}else {
			map1 = new HashMap<String,Object>();
			map1.put("totalPayCount", format);
			map1.put("amountCount", format);
		}
		
		return map1;
	}

}
